package fulbiNet.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;

import fulbiNet.execpcion.DAOExcepcion;
import fulbiNet.modelo.Cancha;
import fulbiNet.util.ConexionBD;

public class CanchaDAO extends BaseDAO {

	
public void insertar(Cancha vo) throws DAOExcepcion {
		System.out.println("CanchaDAO: insertar(Cancha vo)");
		String query = "INSERT INTO Cancha(caracteristicas, tarifa, idLocal, idDueno) VALUES (?,?,?,?)";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			
			stmt.setString(1, vo.getCaracteristicas());
			stmt.setFloat(2, vo.getTarifa());
			stmt.setInt(3, vo.getoLocal().getCodigo());
			stmt.setInt(4, vo.getoDueno().getCodigo());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo insertar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public Cancha obtener(int idCancha) throws DAOExcepcion {
		System.out.println("CanchaDAO: obtener(int idCancha)");
		Cancha vo = new Cancha();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			String query = "select idCancha, caracteristicas, tarifa, idLocal, idDueno from Cancha where idCancha=?";
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idCancha);
			rs = stmt.executeQuery();
			if (rs.next()) {
				
				vo.setCodigo(rs.getInt(1));
				vo.setCaracteristicas(rs.getString(2));
				vo.setTarifa(rs.getFloat(3));
				//vo.setoLocal().getCodigo());
				//vo.setoDueno().getCodigo());
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarResultSet(rs);
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return vo;
	}

	public void eliminar(int idCancha) throws DAOExcepcion {
		System.out.println("CanchaDAO: eliminar(int idCancha)");
		String query = "DELETE FROM Cancha WHERE idCancha=?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setInt(1, idCancha);
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo eliminar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	public void actualizar(Cancha vo) throws DAOExcepcion {
		System.out.println("CanchaDAO: actualizar(Cancha vo)");
		String query = "update Cancha set caracteristicas = ?, tarifa = ?, idLocal = ?, idDueno = ? where idCancha = ?";
		Connection con = null;
		PreparedStatement stmt = null;
		try {
			con = ConexionBD.obtenerConexion();
			stmt = con.prepareStatement(query);
			stmt.setString(1, vo.getCaracteristicas());
			stmt.setFloat(2, vo.getTarifa());
			stmt.setInt(3, vo.getoLocal().getCodigo());
			stmt.setInt(4, vo.getoDueno().getCodigo());
			
			int i = stmt.executeUpdate();
			if (i != 1) {
				throw new SQLException("No se pudo actualizar");
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
	}

	
public Collection<Cancha> listar() throws DAOExcepcion {
		System.out.println("CanchaDAO: listar()");
		Collection<Cancha> c = new ArrayList<Cancha>();
		Connection con = null;
		PreparedStatement stmt = null;
		ResultSet rs = null;
		try {
			con = ConexionBD.obtenerConexion();
			String query = "SELECT idCancha, caracteristicas, tarifa, idLocal, idDueno from Cancha";
			stmt = con.prepareStatement(query);
			rs = stmt.executeQuery();
			while (rs.next()) {
				Cancha vo = new Cancha();
				
				vo.setCodigo(rs.getInt(1));
				vo.setCaracteristicas(rs.getString(2));
				vo.setTarifa(rs.getFloat(3));
				//vo.setoLocal().getCodigo());
				//vo.setoDueno().getCodigo());

				c.add(vo);
			}
		} catch (SQLException e) {
			System.err.println(e.getMessage());
			throw new DAOExcepcion(e.getMessage());
		} finally {
			this.cerrarStatement(stmt);
			this.cerrarConexion(con);
		}
		return c;
	}


public Collection<Cancha> buscar(String scaracteristicas, int ilocal) throws DAOExcepcion {
	System.out.println("CanchaDAO: buscar()");
	Collection<Cancha> c = new ArrayList<Cancha>();
	Connection con = null;
	PreparedStatement stmt = null;
	ResultSet rs = null;
	try {
		con = ConexionBD.obtenerConexion();
		String query = "SELECT idCancha, caracteristicas, tarifa, idLocal, idDueno from Cancha WHERE idlocal = ? and caracteristicas like ?";
		stmt = con.prepareStatement(query);
	
		stmt.setString(1, scaracteristicas);
		stmt.setInt(2, ilocal);
	
		
		rs = stmt.executeQuery();
		while (rs.next()) {
			Cancha vo = new Cancha();
			
			vo.setCodigo(rs.getInt(1));
			vo.setCaracteristicas(rs.getString(2));
			vo.setTarifa(rs.getFloat(3));
			//vo.setoLocal().getCodigo());
			//vo.setoDueno().getCodigo());

			c.add(vo);
		}
	} catch (SQLException e) {
		System.err.println(e.getMessage());
		throw new DAOExcepcion(e.getMessage());
	} finally {
		this.cerrarStatement(stmt);
		this.cerrarConexion(con);
	}
	return c;
}

public void cambiarEstado(Cancha vo) throws DAOExcepcion {
	System.out.println("CanchaDAO: actualizar(Cancha vo)");
	String query = "update Cancha set estado = ? where idCancha = ?";
	Connection con = null;
	PreparedStatement stmt = null;
	try {
		con = ConexionBD.obtenerConexion();
		stmt = con.prepareStatement(query);
		
		stmt.setInt(1, vo.getCodigo());
		stmt.setInt(2, vo.getEstado());
		
		int i = stmt.executeUpdate();
		if (i != 1) {
			throw new SQLException("No se pudo actualizar");
		}
	} catch (SQLException e) {
		System.err.println(e.getMessage());
		throw new DAOExcepcion(e.getMessage());
	} finally {
		this.cerrarStatement(stmt);
		this.cerrarConexion(con);
	}
}

}


